package com.zhuoyue.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zhuoyue.common.annotation.DataScope;

import com.zhuoyue.common.utils.StringUtils;
import com.zhuoyue.system.domain.*;
import com.zhuoyue.system.domain.dto.BaseDeviceMessage;
import com.zhuoyue.system.domain.po.TblAutoMatePo;
import com.zhuoyue.system.domain.vo.AutomationVo;
import com.zhuoyue.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuoyue.system.service.ITblAutomateService;

/**
 * 自动化(联动)Service业务层处理
 *
 * @author hewenze
 * @date 2020-08-10
 */
@Service
public class TblAutomateServiceImpl implements ITblAutomateService {
    @Autowired
    private TblAutomateMapper tblAutomateMapper;
    @Autowired
    private LocalSceneGatewayRelationMapper gatewayRelationMapper;
    @Autowired
    private LocalSceneZigbeeDeviceRelationMapper zigbeeDeviceRelationMapper;
    @Autowired
    private LocalSceneInfraredDeviceRelationMapper infraredDeviceRelationMapper;
    @Autowired
    private LocalSceneGroupRelationMapper groupRelationMapper;
    @Autowired
    private TblDeviceActionsMapper actionsMapper;

    /**
     * 查询自动化(联动)列表
     *
     * @param tblAutoMatePo 自动化(联动)
     * @return 自动化(联动)
     */
    @Override
    @DataScope(deptAlias = "s_d")
    public List<AutomationVo> selectTblAutomateList(TblAutoMatePo tblAutoMatePo) {
        return tblAutomateMapper.selectTblAutomateList(tblAutoMatePo);
    }

    @Override
    public AutomationVo selectById(String id) {
        AutomationVo automationVo = tblAutomateMapper.selectTblAutomateById(id);
        if (automationVo != null && StringUtils.isNotEmpty(automationVo.getTriggerTime())) {
            String start = StringUtils.substring(automationVo.getTriggerTime(), 0, 2);
            String end = StringUtils.substring(automationVo.getTriggerTime(), 2, 4);
            int i1 = Integer.parseInt(start, 16);
            int i2 = Integer.parseInt(end, 16);
            automationVo.setTriggerTime(i1 + "-" + i2);
        }

        List<LocalSceneGatewayRelation> localSceneGatewayRelations = gatewayRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneZigbeeDeviceRelation> localSceneZigbeeDeviceRelations = zigbeeDeviceRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneInfraredDeviceRelation> localSceneInfraredDeviceRelations = infraredDeviceRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneGroupRelation> localSceneGroupRelations = groupRelationMapper.selectByPrimarySceneId(id);
        List<BaseDeviceMessage> baseDeviceMessages = new ArrayList<>(10);
        localSceneGatewayRelations.forEach(g -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(g.getGatewayName(), "网关", g.getGatewayMac());
            baseDeviceMessages.add(baseDeviceMessage);
        });

        localSceneZigbeeDeviceRelations.forEach(z -> {
            List<TblDeviceActions> tblDeviceActions = actionsMapper.listActionsByDeviceId(z.getDeviceId());
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(z.getDeviceName(), z.getDeviceType(), z.getDefaultDeviceName(), z.getZigbeeMac(), tblDeviceActions, z.getControlParam(), z.getStateDes());
            baseDeviceMessages.add(baseDeviceMessage);
        });

        localSceneInfraredDeviceRelations.forEach(i -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(i.getInfraredName(), "红外", "");
            baseDeviceMessages.add(baseDeviceMessage);
        });
        localSceneGroupRelations.forEach(g -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(g.getGroupName(), "群组", "");
            baseDeviceMessages.add(baseDeviceMessage);
        });
        automationVo.setDeviceList(baseDeviceMessages);
        return automationVo;
    }
}
